/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86183
 * Date: 2022-09-23
 * Time: 8:14
 */
public class MyListNode {


    static class Listnode {
        public int val;
        public Listnode next;
        public Listnode perv;

        public Listnode(int val) {
            this.val = val;
        }
    }

    public Listnode head;
    public Listnode tail;

    //头插法
    public void addFirst(int data) {
        Listnode node = new Listnode(data);
        if (this.head == null) {
            this.head = node;
            this.tail = node;
        } else {
            node.next = head;
            this.head.perv = node;
            head = node;
        }

    }

    //尾插法
    public void addLast(int data) {
        Listnode node = new Listnode(data);
        if (this.head == null) {
            this.head = node;
            this.tail = node;
        } else {
            tail.next = node;
            node.perv = tail;
            tail = node;
        }
    }

    //任意位置插入,第一个数据节点为0号下标
    public void addIndex(int index, int data) throws indexWrongfulException {
        if (index < 0 || index > size()) {
            throw new indexWrongfulException("index的位置不合法");
        }
        if (index == 0) {
            addFirst(data);
            return;
        }
        if (index == size()) {
            addLast(data);
            return;
        }
        Listnode cur = toFindindex(index);
        Listnode node = new Listnode(data);
        node.next = cur;
        cur.perv.next = node;
        node.perv = cur.perv;
        cur.perv = node;
    }

    private Listnode toFindindex(int index) {
        Listnode cur = head;
        while (index != 0) {
            cur = cur.next;
            index--;
        }
        return cur;
    }

    //查找是否包含关键字key是否在单链表当中
    public boolean contains(int key) {
        Listnode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }
//        //删除第一次出现关键字为key的节点
        public void remove(int key){
           Listnode cur = head;
           while(cur != null){
               if(cur.val == key){
                   if(cur == head){
                       head = head.next;
                       if(head != null){
                           head.perv = null;
                       }else{
                           tail = null;
                       }
                   }else{

                   cur.perv.next = cur.next;
                   if(cur.next != null){
                       cur.next.perv = cur.perv;
                   }else {
                       this.tail = cur.perv;
                   }
                   }
                   return;
               }
               cur = cur.next;
           }
        }
        //删除所有值为key的节点
        public void removeAllKey(int key){
            Listnode cur = head;
            while(cur != null){
             if(cur.val == key){
              if(cur == head){
                head = head.next;
                if(head != null){
                    head.perv = null;
                }else{
                    tail = null;
                }
                 }else{

                cur.perv.next = cur.next;
                if(cur.next != null){
                    cur.next.perv = cur.perv;
                }else {
                    this.tail = cur.perv;
                }
            }

        }
        cur = cur.next;
    }
    }
//        //得到单链表的长
        public int size(){
            Listnode cur = head;
            int count = 0;
            while (cur != null){
                count++;
                cur =cur.next;
            }
            return count;
        }
        public void display(){
            Listnode cur = head;
            while(cur != null){
                System.out.print(cur.val+" ");
                cur = cur.next;
            }
            System.out.println();
        }
        public void clear(){
        Listnode cur = head;
        while(cur != null){
            Listnode curNext = cur.next;
            cur.perv =null;
            cur.next = null;
            cur = curNext;
        }
        head = null;
        tail = null;
        }


}
